package com.songlanyun.modules.order.service.impl.platformpoin;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.order.model.vo.SubmitChildOrderVo;
import com.songlanyun.modules.order.model.vo.SubmitOrderVo;
import com.songlanyun.modules.order.model.vo.SubmitShopOrderVo;
import com.songlanyun.modules.pointsexchangeconfig.entity.PointsExchangeConfigEntity;
import com.songlanyun.modules.pointsexchangeconfig.service.PointsExchangeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Service("commonOrderUsePlatformPoint")
public class CommonOrderUsePlatformPoint implements IUsePlatformPoint  {
    @Autowired
    private UserIdentityAssetService userIdentityAssetService;

    @Autowired
    private PointsExchangeConfigService pointsExchangeConfigService;


    @Override
    public BigDecimal handle(SubmitOrderVo vo) {
        UserIdentityAssetEntity asset = userIdentityAssetService.getById(vo.getUserId(), true);
        vo.setIntegralAble(asset.getIntegralAble());
        Optional<SubmitShopOrderVo> first = vo.getShopOrders().stream().filter(SubmitShopOrderVo::getPlatformShop).findFirst();
        SubmitShopOrderVo platformShopOrderVo = first.orElse(null);
        if(ObjectUtil.isNull(platformShopOrderVo))
        {
            return BigDecimal.ZERO;
        }

        PointsExchangeConfigEntity config = pointsExchangeConfigService.loadConfig(true);
        Integer integralUseRatio = config.getIntegralUseRatio();

        BigDecimal platformShopDkPoint = MathUtils.setScale(platformShopOrderVo.getTotalAmountNoFreight().doubleValue() * integralUseRatio / 100 ) ;

        if(asset.getIntegralAble().compareTo(platformShopDkPoint)<0)
        {
            platformShopDkPoint = asset.getIntegralAble();
        }
        if(platformShopDkPoint.compareTo(BigDecimal.ZERO)>0) {
            platformShopOrderVo.setTotalPoint(platformShopDkPoint);
            platformShopOrderVo.setTotalAmountDecPlatformPoint( MathUtils.subtract(platformShopOrderVo.getTotalAmountDecPlatformPoint(),platformShopDkPoint));
            platformShopOrderVo.setTotalAmount( MathUtils.subtract(platformShopOrderVo.getTotalAmount(),platformShopDkPoint));
            platformShopOrderVo.setTotalReduced(MathUtils.add(platformShopOrderVo.getTotalReduced(),platformShopDkPoint));
            List<SubmitChildOrderVo> items = platformShopOrderVo.getItems();
            BigDecimal sum = BigDecimal.ZERO;
            for(SubmitChildOrderVo submitChildOrderVo : items)
            {
                BigDecimal goodsAmount = MathUtils.subtractMulti(submitChildOrderVo.getTotalPrice(),
                        submitChildOrderVo.getFirstOrderReduced(),
                        submitChildOrderVo.getShopCouponSharing(),
                        submitChildOrderVo.getPlatformCouponSharing());

                submitChildOrderVo.setPlatformPointZxSharing(MathUtils.setScale(goodsAmount.doubleValue() * platformShopDkPoint.doubleValue() / platformShopOrderVo.getTotalAmountNoFreight().doubleValue()));
                sum = MathUtils.add(submitChildOrderVo.getPlatformPointZxSharing(), sum);
            }

            if (platformShopDkPoint.compareTo(sum) > 0) {
                BigDecimal value = MathUtils.subtract(platformShopDkPoint, sum);
                items.get(0).setPlatformPointZxSharing(MathUtils.add(items.get(0).getPlatformPointZxSharing(), value));
            }
            for(SubmitChildOrderVo submitChildOrderVo : items)
            {
                submitChildOrderVo.setExchangePoint(MathUtils.setScale(submitChildOrderVo.getPlatformPointZxSharing().doubleValue()/submitChildOrderVo.getNum()));
            }

        }




        return platformShopDkPoint;

    }
}
